<!--比价页面-->

<template>

  <div id="table">
    <el-button type="success" @click="openCheck" icon="el-icon-success" v-if="isShowSuditButton && showCheck">审核</el-button>
    <!-- <el-button type="success" @click="openCheck" icon="el-icon-success" v-if="isShowSuditButton">审核</el-button> -->
    <el-table :data="tableData" height="420">
      <el-table-column prop="materialCode" fixed label="物料编码" width="150"></el-table-column>
      <el-table-column prop="materialName" fixed label="物料名称" width="200"></el-table-column>
      <el-table-column prop="purOrgName" label="采购组织名称" width="120"></el-table-column>
      <el-table-column prop="planQty" label="计划采购量" width="100"></el-table-column>
      <el-table-column prop="logistMode" label="物流方式" width="100"></el-table-column>
      <el-table-column prop="operation" label="操作" width="140">
        <template slot-scope="scope">
          <el-button @click="termComparisonClick(scope.row)" type="text" size="small">条款对比</el-button>
          <el-button @click="subentryComparisonClick(scope.row)" type="text" size="small">分项对比</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="lowestPrice" label="上期最低报价" width="120"></el-table-column>
      <el-table-column v-for='item in tableConfig' :label="item.label" :prop='item.prop' :key='item.prop' :width='item.width'>
        <el-table-column v-if='(item.children||item.children.length>0)&&!item1.hidden' v-for="item1 in item.children" :label="item1.label"
          :prop='item1.prop' :width='item1.width' :key="item1.prop">
          <template slot-scope="scope">
            <span v-if="item1.prop.indexOf('quota')<0">{{scope.row[item1.prop]}}</span>
            <template v-else>
              <span v-if="isedit">{{scope.row[item1.prop]}}</span>
              <el-input type="text" v-else v-model="tableData[scope.$index][item1.prop]" />
            </template>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

    <template>

      <el-dialog title="分项对比" :visible.sync="dialogSubentryComparison" left width="80%">
        <Subentry-comparison :SubentryComparisonData="SubentryComparisonData" ref="SubentryComparison"></Subentry-comparison>
      </el-dialog>
    </template>

    <template>
      <el-dialog title="条款对比" :visible.sync="dialogClause" left width="80%">
        <Dialog-Clause :dialogClauseData="dialogClauseData" ref="DialogClause"></Dialog-Clause>
      </el-dialog>
    </template>


    <template>

      <el-dialog title="价格审批" :visible.sync="dialogaUdit" left width="40%">
        <template>
          <div>
            <el-form>
              <el-form-item label="结论" :label-width="formLabelWidth">
                <el-radio-group v-model="submitData.dg_checkStatus">
                  <el-radio :label="1">通过</el-radio>
                  <el-radio :label="0">驳回</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="审核意见" :label-width="formLabelWidth">
                <el-input v-model="submitData.dg_checkContent"></el-input>
              </el-form-item>
              <el-form-item label="审核附件" :label-width="formLabelWidth">
                <el-upload class="upload-demo" drag action="" multiple>
                  <i class="el-icon-upload"></i>
                  <div class="el-upload__text">将文件拖到此处，或
                    <em>点击上传</em>
                  </div>
                  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
              </el-form-item>

              <el-row style="text-align: center">
                <el-button @click="audit" type="success" center>提交</el-button>
                <el-button @click="dialogaUdit = false" type="info" center>取消</el-button>
              </el-row>
            </el-form>
          </div>
        </template>
      </el-dialog>
    </template>
  </div>

</template>
<script>
  import DialogClause from '@/components/rfq/enquiry/enquirysteps/child/dialogClause.vue';
  import SubentryComparison from '@/components/rfq/enquiry/enquirysteps/child/subentryComparison.vue';
  import TableExt from '@/components/common/table-ext/table-ext.vue';
  import SelectTable from '@/components/common/select-ext/select-table';
  import TableIcon from '@/components/common/table-ext/table-icon';
  import qs from 'qs';

  export default {
    components: {
      TableIcon,
      'Subentry-comparison': SubentryComparison,
      'Dialog-Clause': DialogClause,
    },
    data() {

      let _this = this;
      return {
        showCheck : false,
        checkId : '',
        lastNode : '',
        isShowSuditButton: true,
        dialogaUdit: false,
        dialogClause: false,
        dialogClauseData: [],
        dialogSubentryComparison: false,
        SubentryComparisonData: [],
        isedit: true,
        isSaveEdit: false,
        activeName2: 'first',
        isShow: false,
        tableData: [],
        tableConfig: [],
        submitData: {
          dg_checkStatus: 1,
          dg_checkContent: '',
          dg_checkAttachment: ''
        },
        formLabelWidth: '100px',
      };
    },
    created() {
      var rfqId = this.$route.query.id; //询价单ID
      this.axios.post(this.lib.config.host.rfq + '/common/flowcheck/checkAuth', {tableId : this.$route.query.id}).then((res)=>{
          if(res.data.success && res.data.resultMap.flag == 'true'){
            this.showCheck = true;
            this.checkId = res.data.resultMap.checkId;
            this.lastNode = res.data.resultMap.lastNode;
          }
        });
      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/rfqEnquiry/' + this.$route.query.id).then((res) => {
        if (res.data.success && res.data.checkWinStatus === 3) {
          this.isShowSuditButton = true
        }
      })


      //比价分析-动态表头
      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/buildQuotaGrid/' + rfqId).then((res) => {
        if (res.data.success) {
          var dynamicColumns = res.data.result.dynamicColumns;
          this.tableConfig = dynamicColumns;
        }
      }).catch(function (error) {
        console.log(error);
      });

      //比价分析数据
      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/buildQuotaGridData/' + rfqId).then((res) => {
        console.log(res.data.result);
        if (res.data.success) {
          this.tableData = res.data.result;
        }
      }).catch(function (error) {
        console.log(error);
      });


    },

    methods: {
      //审核  按钮单击
      openCheck(){
        this.dialogaUdit = true;
      },
      audit() {
        if(this.submitData.dg_checkStatus == ''){
          this.$message.info('审核结果不能为空！');
          return;
        }
          if(this.submitData.dg_checkStatus == -1 && this.submitData.dg_checkContent == ''){
            this.$message.info('审核意见不能为空！'); 
          }else {
          // dg_checkStatus:驳回状态  1 /0
          // dg_checkContent  驳回 信息
          // dg_checkAttachment:附件 可为空
          this.axios.post(this.lib.config.host.rfq+'/common/flowcheck/check', {id : this.checkId, checkStatus : this.submitData.dg_checkStatus, option : this.submitData.dg_checkContent}).then((res)=>{
          if(res.data.success){
            if((this.submitData.dg_checkStatus == -1 && this.lastNode == 2) || this.lastNode == 1){
              let curl = this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/check/checkPrice/' + this.$route.query.id
              this.axios.post(curl, qs.stringify(this.submitData)).then((res) => {
                if (res.data.success) {
                  this.dialogaUdit = false;
                  this.$message.info('操作成功!');
                }
              }).catch(function (error) {
                console.log(error);
                });
              }else{
                this.dialogaUdit = false;
                this.$message.info('操作成功!');
              }
            }
          });


        // let curl = this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/check/checkPrice/' + this.$route.query.id
        //       this.axios.post(curl, qs.stringify(this.submitData)).then((res) => {
        //         if (res.data.success) {
        //           this.$message.info('操作成功!');
        //         }
        //       }).catch(function (error) {
        //         console.log(error);
        //       });
        }
      },


      //配额设置
      quotaSetting() {
        this.isedit = false; //配额设置
        this.isSaveEdit = true; //保存配额
      },
      //取消配额设置
      cancelQuota() {
        this.isedit = true; //配额设置
        this.isSaveEdit = false; //保存配额
      },

      submitForm() {
        let data = [];
        let validFlag = false;
        this.tableData.forEach((item, index) => {
          debugger;
          var vendorCodeList = item.vendorCodeList; //供应商ID列表
          let suggestQuotaTotal = 0;
          validFlag = false;
          for (var i = 0; i < vendorCodeList.length; i++) {
            var venMatObj = {};
            Object.keys(item).forEach(k => {
              if (k.indexOf('venMatId_' + vendorCodeList[i]) >= 0) {
                venMatObj.id = item[k]; //询价供应商物料ID
                console.log('venMatId_')
                console.log(venMatObj.id)
              }
              if (k.indexOf('quota_' + vendorCodeList[i]) >= 0) {
                venMatObj.suggestQuota = item[k]; //建议配额
                suggestQuotaTotal += parseFloat(venMatObj.suggestQuota);
                console.log('venMatId_')
                console.log(venMatObj.id)
              }
            })
            data.push(venMatObj);
          }
          if (suggestQuotaTotal != 100) {
            validFlag = true;
            this.$message.info('物料名称：【' + item.materialName + '】的所有供应商配额合计必须为100！');
            return;
          }
        })
        if (validFlag) {
          return;
        }
        this.isedit = true; //配额设置
        this.isSaveEdit = false; //保存配额
        // console.log(data);

        //保存配额提交
        this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/compareQuota/saveQuota', data).then((res) => {
          if (res.data.success) {
            this.$message.info('操作成功!');
          }
        }).catch(function (error) {
          console.log(error);
        });
      },


      //提交价格审核
      submitPriceCheck() {
        let data = {};
        // let priceCheckIds = [];
        // priceCheckIds.push(this.$route.query.id);
        data.priceCheckIds = this.$route.query.id;
        data.dg_checkContent = '测试审核内容';
        this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/submitPriceCheck', qs.stringify(data)).then(
          (res) => {
            if (res.data.success) {
              this.$message.info('操作成功!');
            }
          }).catch(function (error) {
          console.log(error);
        });
      },
      //条款对比
      termComparisonClick(row) {
        this.dialogClause = true;
        row.enquiryId = this.$route.query.id;
        this.dialogClauseData = row;

      },
      //分项对比
      subentryComparisonClick(row) {
        this.dialogSubentryComparison = true;
        row.enquiryId = this.$route.query.id;
        this.SubentryComparisonData = row;


      },
      handleClick(tab, event) {
        console.log(tab, event);
      }
    }
  }

</script>
